package com.fashion.service.gallery;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

import com.fashion.model.gallery.GalleryColumn;
import com.fashion.utils.StringUtils;
import com.fashion.utils.Variable;

@Service("galleryColumnService")
public class GalleryColumnService {

	@Resource(name = "hibernateTemplate")
	private HibernateTemplate template;

	public List<GalleryColumn> findAll() {
		List<GalleryColumn> cloumns = template.find("from GalleryColumn");
		return cloumns;
	}

	public GalleryColumn findById(String id) {
		return template.get(GalleryColumn.class, id);
	}

	public void addColumn(GalleryColumn column) {
		column.setId(StringUtils.createGuid());
		column.setDate(new Date());
		template.save(column);
	}

	public void delColumn(GalleryColumn column) {
		template.delete(column);
	}

	public void updateColumn(GalleryColumn column) {
		column.setDate(new Date());
		template.update(column);
	}

	public List<GalleryColumn> format(List<GalleryColumn> columns) {
		List<GalleryColumn> columnTrees = new ArrayList<GalleryColumn>();

		for (int i = 0; i < columns.size(); i++) {
			GalleryColumn column = columns.get(i);
			if (column.getParentId().equals(Variable.GALLERY_DB_ID)) {
				columnTrees.add(column);
				continue;
			}
			for (int j = 0; j < columns.size(); j++) {
				GalleryColumn parent = columns.get(j);
				if (column.getParentId().equals(parent.getId())) {
					List<GalleryColumn> children = parent.getChildren();
					children.add(column);
					continue;
				}
			}
		}
		return columnTrees;
	}
	
	public GalleryColumn findColumnFromTree(List<GalleryColumn> trees, GalleryColumn galleryColumn){
		GalleryColumn column;
		for(GalleryColumn tree : trees){
			if(tree.getId().equals(galleryColumn.getId())){
				return tree;
			}
			List<GalleryColumn> children = tree.getChildren();
			if (children != null){
				column = findColumnFromTree(children, galleryColumn) ;
				if (column != null){
					return column;
				}
			}
		}
	    return null;
	}
	
	public List<GalleryColumn> getAllChildren(GalleryColumn galleryColumn){
	    List<GalleryColumn> list = new ArrayList<GalleryColumn>();
	    List<GalleryColumn> children = galleryColumn.getChildren();
	    if (children != null)
	        for(int i = 0; i < children.size(); i++){
	            list.add(children.get(i));
	            list.addAll(getAllChildren(children.get(i)));
	        }
	    return list;
	}
	
	public List<GalleryColumn> getAllParents(GalleryColumn galleryColumn){
		List<GalleryColumn> list = new ArrayList<GalleryColumn>();
		String id = galleryColumn.getParentId();
		if( id == null || Variable.GALLERY_DB_ID.equals(id))
			return list;
		GalleryColumn parent = findById(id);
		list.add(parent);
		getAllParents(parent);
		return list;
	}
}
